<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">    
    <title>IFE ECMAScript</title>
</head>
<body>        
    <input id="dec-number" type="number" placeholder="输入一个十进制非负整数">
    <input id="bin-bit" type="number" placeholder="输入转化后二进制数字位数">
    <button id="trans-btn" >转化为二进制</button>
    <p id="result">运算结果</p>
    <script>

function dec2bin(decNumber) {
    var decStack = [];
    var rem;
    var decString = '';
    while (decNumber > 0){
        rem = decNumber % 2;
        decStack.push(rem);
        decNumber = Math.floor(decNumber/2);
    }
    
    while (decStack.length != 0){
        decString += decStack.pop().toString();
    }
    console.log(decString);
    document.getElementById('result').innerHTML = '运算结果' + decString;
    return decString;
}
function getval(){
    var val = document.getElementById('dec-number').value;
    if(val < 1){
        alert('输入数字不能小于1')
    }else{
        dec2bin(val);
    }
}
document.getElementById('trans-btn').addEventListener('click',getval);

// 实现党点击转化按钮时，将输入的十进制数字转化为二进制，并显示在result的p标签内
// 新的需求是，转化显示后的二进制数为bin-bit中输入的数字宽度，例如
// dec-number为5，bin-bit为5，则转化后数字为00101
// 如果bin-bit小于转化后的二进制本身位数，则使用原本的位数，如dec-number为5，bin-bit为2，依然输出101，但同时在console中报个错
// Some coding

    </script>
</body>
</html>